using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using __Naptime.Steam;

namespace Naptime.Dlc
{
	public sealed class DLCManager : Singleton<DLCManager>
	{
		[Serializable]
		public class FirstInitializedDLCContainer
		{
			public List<FirstInitializedDLC> firstInitializedDLCs = new List<FirstInitializedDLC>();
		}

		[Serializable]
		public sealed class FirstInitializedDLC
		{
			public string id;

			public bool initialized;

			public FirstInitializedDLC(string id, bool initialized)
			{
				this.id = id;
				this.initialized = initialized;
			}
		}

		private const string SAVE_KEY = "DLCManagerDATA";

		[SerializeField]
		private List<DLCData> gamesDLCS = new List<DLCData>();

		[SerializeField]
		private List<string> _disabledEditorDLCs = new List<string>();

		private readonly List<DLCData> _installedDLCs = new List<DLCData>();

		private FirstInitializedDLCContainer _firstInitializedDLCsContainer = new FirstInitializedDLCContainer();

		private bool _isSaveLoaded;

		public event Action OnDataLoaded;

		private void Update()
		{
			if (!_isSaveLoaded && NintendoPlayerPrefs.IsInit)
			{
				LoadSavedData();
				RefreshAOCList();
				_isSaveLoaded = true;
				this.OnDataLoaded?.Invoke();
			}
		}

		public void RefreshAOCList()
		{
			AddDLCContentToGame();
		}

		private List<DLCData> GetInstalledDLC()
		{
			return GetSteamDlc();
		}

		private List<DLCData> GetEditorDLC()
		{
			return gamesDLCS.Where((DLCData elem) => !_disabledEditorDLCs.Contains(elem.ID)).ToList();
		}

		private List<DLCData> GetSteamDlc()
		{
			List<DLCData> list = new List<DLCData>();
			if (Singleton<SteamAOCManager>.Instance.dlc1 && gamesDLCS.Count >= 1)
			{
				list.Add(gamesDLCS[0]);
			}
			if (Singleton<SteamAOCManager>.Instance.dlc2 && gamesDLCS.Count >= 2)
			{
				list.Add(gamesDLCS[1]);
			}
			if (Singleton<SteamAOCManager>.Instance.dlc3 && gamesDLCS.Count >= 3)
			{
				list.Add(gamesDLCS[2]);
			}
			return list;
		}

		private void AddDLCContentToGame()
		{
			foreach (DLCData dlc in GetInstalledDLC())
			{
				if (_installedDLCs.Find((DLCData d) => d.ID == dlc.ID) == null)
				{
					_installedDLCs.Add(dlc);
					AddInitializedDLCs(dlc);
				}
			}
			Save();
		}

		private void LoadSavedData()
		{
		}

		private void Save()
		{
			SaveInitializedDLC();
		}

		private void SaveInitializedDLC()
		{
		}

		private void AddInitializedDLCs(DLCData data)
		{
			if (_firstInitializedDLCsContainer.firstInitializedDLCs.Find((FirstInitializedDLC f) => f.id == data.ID) == null)
			{
				_firstInitializedDLCsContainer.firstInitializedDLCs.Add(new FirstInitializedDLC(data.ID, initialized: true));
			}
		}

		public bool HasAllDLC()
		{
			return _installedDLCs.Count == gamesDLCS.Count;
		}

		public bool HasDlc(int nintendoId)
		{
			return _installedDLCs.Any((DLCData e) => e.ID == nintendoId.ToString());
		}

		public bool IsLevelDlc(LevelPreset level)
		{
			return gamesDLCS.Any((DLCData e) => e.Levels.Contains(level));
		}

		public bool IsSkinDlc(Item item)
		{
			return gamesDLCS.Any((DLCData e) => e.Skins.Contains(item));
		}

		public bool IsSkinUnlocked(Item item)
		{
			return _installedDLCs.Any((DLCData e) => e.Skins.Contains(item));
		}

		public bool IsLevelUnlocked(LevelPreset level)
		{
			return _installedDLCs.Any((DLCData e) => e.Levels.Contains(level));
		}
	}
}
